[gd_scene load_steps=8 format=2]

[ext_resource path="res://blokoj/kosmo/shaders/space.gdshader" type="Shader" id=1]
[ext_resource path="res://blokoj/kosmo/resursoj/space_env.tres" type="Environment" id=2]
[ext_resource path="res://blokoj/kosmo/shaders/space.png" type="Texture2D" id=3]
[ext_resource path="res://blokoj/kosmo/shaders/noise.png" type="Texture2D" id=4]


[sub_resource type="GDScript" id=1]
script/source = "extends Node3D
var iTime = 0.0
var cam
var mega_sun = false
func _ready():
	cam = get_tree().get_root().get_camera_3d() # получаем камеру
	$Env.environment.background_sky.set_panorama($space_viewport.get_texture())
	
func _process(delta):
	iTime +=delta
	$space_viewport/space_texture.material.set(\"shader_param/iTime\",iTime)
	_set_sun_position()
	
func _set_sun_position():
	var position = cam.get_position()
	if mega_sun:
		$space_viewport/space_texture.material.set(\"shader_param/sun_radius\",clamp((4000-position.distance_to(Vector3.ZERO))*0.025,0.1,100))
	position = position.normalized()
	#position.x *= -1
	position.y *= -1
	position.z *= -1
	$space_viewport/space_texture.material.set(\"shader_param/SUN_POS\",position)
	position.x *= -1
	$star.look_at_from_position(position,Vector3.ZERO,Vector3.UP);


func _on_CheckBox_toggled(button_pressed):
	mega_sun = button_pressed

func _on_HScrollBar_value_changed(value):
	$space_viewport/space_texture.material.set(\"shader_param/noise_freq\",value)
	print(value)
"

[sub_resource type="ShaderMaterial" id=2]
resource_local_to_scene = true
shader = ExtResource( 1 )
shader_param/iTime = 0.0
shader_param/sun_radius = 20.0
shader_param/noise_freq = 10.0
shader_param/SUN_POS = Vector3( 0, 0, -1 )
shader_param/sun_color = Color( 0.933333, 0.792157, 0.0313726, 1 )
shader_param/Noise = ExtResource( 4 )
shader_param/space_env_texture = ExtResource( 3 )

[sub_resource type="ImageTexture" id=3]
flags = 0
lossy_quality = 1.0
flags = 0
size = Vector2( 1024, 1024 )

[node name="space" type="Node3D"]
script = SubResource( 1 )

[node name="Env" type="WorldEnvironment" parent="."]
environment = ExtResource( 2 )

[node name="space_viewport" type="SubViewport" parent="."]
size = Vector2( 1024, 1024 )
render_target_update_mode = 3

[node name="space_texture" type="Sprite2D" parent="space_viewport"]
material = SubResource( 2 )
texture = SubResource( 3 )
centered = false

[node name="star" type="DirectionalLight3D" parent="."]

[node name="Control" type="Control" parent="."]
offset_right = 40.0
offset_bottom = 40.0
__meta__ = {
"_edit_use_anchors_": false
}

[node name="Label2" type="Label" parent="Control"]
offset_left = 16.6958
offset_top = 492.868
offset_right = 80.6958
offset_bottom = 506.868
text = "sun_noise"
__meta__ = {
"_edit_use_anchors_": false
}

[node name="HScrollBar" type="HScrollBar" parent="Control/Label2"]
offset_left = 0.735092
offset_top = 16.1721
offset_right = 208.735
offset_bottom = 28.1721
__meta__ = {
"_edit_use_anchors_": false
}

[node name="CheckBox" type="CheckBox" parent="Control"]
offset_left = 15.938
offset_top = 458.781
offset_right = 114.938
offset_bottom = 482.781
text = "MEGA_SUN"
__meta__ = {
"_edit_use_anchors_": false
}
[connection signal="value_changed" from="Control/Label2/HScrollBar" to="." method="_on_HScrollBar_value_changed"]
[connection signal="toggled" from="Control/CheckBox" to="." method="_on_CheckBox_toggled"]
